#pragma once

#include <iostream>
#include <cassert>
using namespace std;

namespace xsc
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}


		const_iterator begin() const
		{
			return _str;
		}

		const_iterator end() const
		{
			return _str + _size;
		}

		/*string()
		{
			_str = new char('\0');
			_size = 0;
			_capacity = 0;
		}

		string(const char* s)
		{
			size_t len = strlen(s);
			_str = new char[len + 1];
			_size = len;
			_capacity = len;

			strcpy(_str, s);
		}*/

		string(const char* s = "")
		{
			size_t len = strlen(s);
			_str = new char[len + 1];
			_size = len;
			_capacity = len;

			strcpy(_str, s);
		}
		//s1(s)
		/*string(const string& s)
		{
			_str = new char[s._capacity + 1];
			strcpy(_str, s.c_str());
			_size = s._size;
			_capacity = s._capacity;
		}*/

		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}

		string(const string& s)
		{
			string tmp(s.c_str());
			swap(tmp);
		}

		//s1 = s
		//string& operator=(const string& s)
		//{
		//	//避免自己对自己赋值
		//	if (this != &s)
		//	{
		//		delete[]_str;

		//		_str = new char[s._capacity + 1];
		//		strcpy(_str, s._str);
		//		_size = s._size;
		//		_capacity = s._capacity;
		//	}
		//	return *this;
		//}

		//s1 = s
		/*string& operator=(const string& s)
		{
			string tmp(s.c_str());
			swap(tmp);

			return *this;
		}*/

		//s1 = s
		string& operator=(string s)
		{
			swap(s);

			return *this;
		}

		~string()
		{
			delete[]_str;
			_str = nullptr;
			_size = 0;
			_capacity = 0;
		}

		const char* c_str() const
		{
			return _str;
		}

		size_t size() const
		{
			return _size;
		}

		size_t capacity() const
		{
			return _capacity;
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size && pos >= 0);

			return _str[pos];
		}

		const char& operator[](size_t pos) const
		{
			assert(pos < _size && pos >= 0);

			return _str[pos];
		}

		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}

		void reserve(size_t n);
		void push_back(char ch);
		void append(const char* s);
		string& operator+=(char ch);
		string& operator+=(const char* s);


		void insert(size_t pos, char ch);
		void insert(size_t pos, const char* s);
		void erase(size_t pos, size_t len = npos);

		size_t find(char ch, size_t pos = 0);
		size_t find(const char* s, size_t pos = 0);
		string substr(size_t pos, size_t len = npos);

	private:
		char* _str = nullptr;
		size_t _size = 0;
		size_t _capacity = 0;

		static const size_t npos;
	};

	bool operator<(const string& s1, const string& s2);
	bool operator<=(const string& s1, const string& s2);
	bool operator>(const string& s1, const string& s2);
	bool operator>=(const string& s1, const string& s2);
	bool operator==(const string& s1, const string& s2);
	bool operator!=(const string& s1, const string& s2);

	ostream& operator<<(ostream& out, const string& s);
	istream& operator>>(istream& in, string& s);
}